// 默认画笔颜色
const DefaultStrokeStyle = '#000';
// 默认荧光笔颜色
const FluorescentStrokeStyle = '#FC595E';
// 默认画笔粗细
const DefaultLineWidth = 5;
// 默认荧光笔粗徐
const FluorescentLineWidth = 20;
// 默认橡皮擦粗细
const DefaultEraserLineWidth = 20;

// 默认文本颜色
const DefaultTextColor = '#000';
// 默认字体大小
const DefaultFontSize = 30;
// 默认水平对齐方式
const DefaultTextHorizontalAlign = 'left';
// 默认垂直对齐方式
const DefaultTextVerticalAlign = 'center';
// 默认字体粗细
const DefaultFontWeight = 'normal';

const {
    JMCData
} = require('./jmc-data');

const {
    EditorData
} = require('./editor-data');

/**
 * 里面存储画笔粗细、画笔颜色、荧光笔颜色、等等
 * 需要修改画笔颜色时只修改此单例里的属性值就行，在path对象实例化时会从此单例中取
 */
class EditorShared {

    static className() {
        return 'EditorShared';
    }

    constructor() {
        this._eraserLineWidth = DefaultEraserLineWidth;
        this._currentDataIndex = 0;
        this._prevDataIndex = 0;
    }

    get editor() {
        return this._editor;
    }

    set editor(value) {
        this._editor = value;
    }

    /**
     * 数据管理
     */
    addNewEditorData(atIndex, data) {
        if (data && data.className === EditorData.className()) {
            if (atIndex !== undefined && atIndex !== null && atIndex < this.jmcData.data.length) {
                this.jmcData.data.splice(atIndex, 0, data);
                this.currentDataIndex = atIndex;
            } else {
                this.jmcData.data.splice(this.jmcData.data.length, 0, data);
                this.currentDataIndex++;
            }
            this.onDataChanged && this.onDataChanged(this.jmcData, data);
        } else {
            console.warn(`addNewEditorData invalid data atIndex:${atIndex}, data:${data}`);
        }
        return data;
    }

    replaceEditorData(atIndex, data) {
        if (data && data.className === EditorData.className()) {
            if (atIndex !== undefined && atIndex !== null && atIndex < this.jmcData.data.length) {
                this.jmcData.data.splice(atIndex, 1, data);
                this.currentDataIndex = atIndex;
                this.onDataChanged && this.onDataChanged(this.jmcData, data);
            } else {
                console.warn(`replaceEditorData invalid index atIndex:${atIndex}, data:${data}`);
            }
        } else {
            console.warn(`replaceEditorData invalid data atIndex:${atIndex}, data:${data}`);
        }
        return data;
    }

    addNewJMCData(atIndex, data) {
        if (data && data.className === JMCData.className() && Array.isNotEmpty(data.data)) {
            this.addNewEditorDatas(atIndex, data.data);
        } else {
            console.warn(`addNewJMCData atIndex:${atIndex}, data:${data}`);
        }
        return data;
    }

    addNewEditorDatas(atIndex, datas) {
        if (atIndex !== undefined && atIndex !== null && atIndex < this.jmcData.data.length) {
            this.jmcData.data.splice(atIndex, 0, ...datas);
            this.currentDataIndex = atIndex;
        } else {
            this.jmcData.data.pushFromArray(datas);
            this.currentDataIndex++;
        }
        this.onDataChanged && this.onDataChanged(this.jmcData, datas);
    }

    deleteCurrentData() {
        this.deleteData(this.currentDataIndex);
    }

    deleteData(atIndex) {
        if (atIndex !== undefined && atIndex !== null && atIndex < this.jmcData.data.length) {
            this.jmcData.data.splice(atIndex, 1);
            this.currentDataIndex = Math.min(atIndex, this.jmcData.data.length - 1);
        }
    }

    get jmcData() {
        if (!this._jmcData) {
            this._jmcData = JMCData.singlePageJMCData();
        }
        return this._jmcData;
    }

    set jmcData(value) {
        if (!value || !Array.isNotEmpty(value.data)) {
            value = JMCData.singlePageJMCData();
        }
        this.currentDataIndex = 0;
        this._jmcData = value;
        this.onJMCDataChanged && this.onJMCDataChanged(value);
    }

    currentData() {
        return this.jmcData.data[this.currentDataIndex];
    }

    get currentDataIndex() {
        return this._currentDataIndex;
    }

    set currentDataIndex(value) {
        this._prevDataIndex = Math.max(0, this._currentDataIndex);
        this._currentDataIndex = value;
        this.onCurrentDataIndexChanged && this.onCurrentDataIndexChanged(this._currentDataIndex, this._prevDataIndex);
    }

    get prevDataIndex() {
        return this._prevDataIndex;
    }

    set prevDataIndex(value) {
        this._prevDataIndex = value;
    }

    currentMusicScoreData() {
        return this.currentData().musicScoreData;
    }

    get onJMCDataChanged() {
        return this._onJMCDataChanged;
    }

    set onJMCDataChanged(value) {
        this._onJMCDataChanged = value;
    }

    get onDataChanged() {
        return this._onDataChanged;
    }

    set onDataChanged(value) {
        this._onDataChanged = value;
    }

    /**
     *************************************** draw **************************************
     */
    /**
     * 画笔颜色
     * @returns {*}
     */
    get strokeStyle() {
        if (!this._strokeStyle) {
            this._strokeStyle = DefaultStrokeStyle;
        }
        return this._strokeStyle;
    }

    set strokeStyle(value) {
        this._strokeStyle = value;
    }

    /**
     * 画笔粗细
     * @returns {*}
     */
    get lineWidth() {
        if (!this._lineWidth) {
            this._lineWidth = DefaultLineWidth;
        }
        return this._lineWidth;
    }

    set lineWidth(value) {
        this._lineWidth = value;
    }

    /**
     * 荧光笔颜色
     * @returns {string|*}
     */
    get fluorescentStrokeStyle() {
        if (!this._fluorescentStrokeStyle) {
            this._fluorescentStrokeStyle = FluorescentStrokeStyle;
        }
        return this._fluorescentStrokeStyle;
    }

    set fluorescentStrokeStyle(value) {
        this._fluorescentStrokeStyle = value;
    }

    /**
     * 荧光笔粗徐
     * @returns {number|*}
     */
    get fluorescentLineWidth() {
        if (!this._fluorescentLineWidth) {
            this._fluorescentLineWidth = FluorescentLineWidth;
        }
        return this._fluorescentLineWidth;
    }

    set fluorescentLineWidth(value) {
        this._fluorescentLineWidth = value;
    }

    /**
     * 橡皮擦粗细
     * @returns {*}
     */
    get eraserLineWidth() {
        if (!this._eraserLineWidth) {
            this._eraserLineWidth = DefaultEraserLineWidth;
        }
        return this._eraserLineWidth;
    }

    set eraserLineWidth(value) {
        this._eraserLineWidth = value;
    }

    /**
     *************************************** text **************************************
     */
    /**
     * 文本字体大小
     * @returns {*}
     */
    get fontSize() {
        if (!this._fontSize) {
            this._fontSize = DefaultFontSize;
        }
        return this._fontSize;
    }

    set fontSize(value) {
        this._fontSize = value;
    }

    /**
     * 文本字体
     * @returns {*}
     */
    get font() {
        return this._font;
    }

    set font(value) {
        this._font = value;
    }

    /**
     * 文本水平对齐方式
     * @returns {*}
     */
    get horizontalAlign() {
        if (!this._horizontalAlign) {
            this._horizontalAlign = DefaultTextHorizontalAlign;
        }
        return this._horizontalAlign;
    }

    set horizontalAlign(value) {
        this._horizontalAlign = value;
    }

    /**
     * 文本垂直对齐方式
     * @returns {*}
     */
    get verticalAlign() {
        if (!this._verticalAlign) {
            this._verticalAlign = DefaultTextVerticalAlign;
        }
        return this._verticalAlign;
    }

    set verticalAlign(value) {
        this._verticalAlign = value;
    }

    /**
     * 文本字体粗细
     * @returns {*}
     */
    get fontWeight() {
        if (!this._fontWeight) {
            this._fontWeight = DefaultFontWeight;
        }
        return this._fontWeight;
    }

    set fontWeight(value) {
        this._fontWeight = value;
    }

    /**
     * 文本颜色
     * @returns {*}
     */
    get textColor() {
        if (!this._textColor) {
            this._textColor = DefaultTextColor;
        }
        return this._textColor;
    }

    set textColor(value) {
        this._textColor = value;
    }

    get getBottomLeftVUEComponent() {
        return this._getBottomLeftVUEComponent;
    }

    set getBottomLeftVUEComponent(value) {
        this._getBottomLeftVUEComponent = value;
    }
}

module.exports = {
    EditorShared
};
